package com.dunqi.gpm.cangxi.home.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseFragment;
import com.dunqi.gpm.cangxi.common.base.Data;
import com.dunqi.gpm.cangxi.common.util.CommonUtil;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.cangxi.home.activity.Video2Activity;
import com.dunqi.gpm.cangxi.home.adapter.VideoListAdapter;
import com.dunqi.gpm.cangxi.home.bean.Page;
import com.dunqi.gpm.cangxi.home.bean.Video;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.widget.EmptyView;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2018/1/7.
 */

public class VideoListFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {


    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    private long projectId;
    ArrayList<Video> data = new ArrayList<>();
    private VideoListAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryWebcamList(projectId,true)
                .compose(RxSchedulers.<Page<Video>>io_main())
                .subscribe(new Consumer<Page<Video>>() {
                    @Override
                    public void accept(Page<Video> videoPage) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        ArrayList<Video> list = videoPage.getList();
                        adapter.replaceData(list);


                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);

                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        SwipeUtil.init(swipeRefresh);

        recycler.setLayoutManager(new LinearLayoutManager(context));
        adapter = new VideoListAdapter(data);
        recycler.setAdapter(adapter);

        adapter.setEmptyView(new EmptyView(context));

        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));

        swipeRefresh.setOnRefreshListener(this);

        adapter.setOnItemClickListener(this);

    }

    @Override
    public void onRefresh() {
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        final Video video = data.get(position);

        final ProgressDialog dialog = ProgressDialog.show(context, "提示", "正在跳转");
        ApiFactory.getInstance()
                .getWebcamLiveInfo(video.getProjectWebcamId())
                .compose(RxSchedulers.<Data<String>>io_main())
                .subscribe(new Consumer<Data<String>>() {
                    @Override
                    public void accept(Data<String> stringData) throws Exception {
                        String mainUrl = stringData.getData();
                        String sonUrl = stringData.getData2();
                        Video2Activity.start(getContext(), mainUrl,sonUrl,video.getWebcamName());
                        dialog.dismiss();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        CommonUtil.showSingleToast("跳转失败...");
                        dialog.dismiss();
                    }
                });


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video_list;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        projectId = arguments.getLong(Constance.PROJECTID, -1);
    }

    public static VideoListFragment newInstance(long projectId) {
        VideoListFragment fragment = new VideoListFragment();
        Bundle bundle = new Bundle();
        bundle.putLong(Constance.PROJECTID, projectId);
        fragment.setArguments(bundle);
        return fragment;

    }

}
